﻿using System;
using System.Collections;
using System.Collections.Generic;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using Model;

public partial class ViewbyProduct_FailCategorybyTimeCID : System.Web.UI.Page
{
    private DateTime dt1, dt2;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCustoms();
            this.CrystalReportViewer1.Visible = false;
            this.CrystalReportViewer2.Visible = false;
        }
        this.Page.Unload += new EventHandler(Page_Unload);
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
        this.CrystalReportSource2.Dispose();
    }

    protected void BindCustoms()
    {
        Customer cust = new Customer();
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt1 = DateTime.Parse(this.TextBox1.Value.Trim());
        dt2 = DateTime.Parse(this.TextBox2.Value.Trim());
        ForCrystalReport();
        ForCrystalReport1();
        this.CrystalReportViewer1.Visible = true;
        this.CrystalReportViewer2.Visible = true;
    }

    protected void ForCrystalReport()
    {
        CrystalReportSource1.ReportDocument.Load(Server.MapPath("ViewFailTests.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestInfo> list = GetTestData();
        CrystalReportSource1.ReportDocument.SetDataSource(list);
        CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();
    }

    public IList<TestInfo> GetTestData()
    {
        IList<TestInfo> tests = new List<TestInfo>();
        Test test = new Test();
        string var_productTypeID = this.DropDownList1.SelectedValue;
        tests = test.GetFailTestsbyCID_DT(var_productTypeID, dt1, dt2);
        return tests;
    }

    protected void ForCrystalReport1()
    {
        CrystalReportSource2.ReportDocument.Load(Server.MapPath("ViewFailTestItems.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource2.ReportDocument.ReportDefinition.ReportObjects["Text12"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestItemInfo> list = GetTestItemData();
        CrystalReportSource2.ReportDocument.SetDataSource(list);
        CrystalReportSource2.DataBind();
        this.CrystalReportViewer2.ReportSource = CrystalReportSource2;
        CrystalReportViewer2.DataBind();
    }

    public IList<TestItemInfo> GetTestItemData()
    {
        IList<TestItemInfo> testItems = new List<TestItemInfo>();
        TestItem testItem = new TestItem();
        string var_productTypeID = this.DropDownList1.SelectedValue;
        testItems = testItem.GetFailTestItemsbyCID(var_productTypeID, dt1, dt2);
        return testItems;
    }
}
